package com.gome.oa.dialog;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Toolkit;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;

import com.gome.oa.custom.GMBrowser;
import com.gome.oa.custom.JPanelWithHeight;
import com.gome.oa.listener.BrowserStatusListener;
import com.gome.oa.listener.EssLoadActionListener;
import com.gome.oa.listener.FillFormActionListener;
import com.gome.oa.listener.PopupNavigationAdapter;
import com.gome.oa.listener.PrepareActionListner;
import com.gome.oa.util.DateUtil;
import com.teamdev.jexplorer.Browser;
import com.teamdev.jexplorer.event.DisposeListener;

/**
 *
 * @author: tang li
 * @date: 2016年8月13日
 * @desc: 
 *
 */
public class PopupBrowserDialog extends JDialog {

	private static final long serialVersionUID = 3209821589374907965L;
	
	private JFrame mainFrame;
	
	private JLabel lblStatusBar = new JLabel();
	
	private GMBrowser browser;
	
	private JPanel topPosition = new JPanel();

	public PopupBrowserDialog(JFrame mainFrame, final GMBrowser browser) {
		super(mainFrame, true);
		this.mainFrame = mainFrame;
		this.browser = browser;
		browser.setOwner(this);
		lblStatusBar.setPreferredSize(new Dimension(200, 20));
		setLayout(new BorderLayout());
		Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
		setSize(d.width, d.height - 50);
		initDynamicComponent();
		getContentPane().add(browser, BorderLayout.CENTER);
    	getContentPane().add(lblStatusBar, BorderLayout.SOUTH);
        setLocationRelativeTo(null);

        addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                browser.dispose();
            }
        });

        browser.addStatusListener(new BrowserStatusListener(PopupBrowserDialog.this, lblStatusBar));
        browser.addNavigationListener(new PopupNavigationAdapter(topPosition));
        browser.addDisposeListener(new DisposeListener() {
            @Override
            public void onDispose(Browser browser) {
                SwingUtilities.invokeLater(new Runnable() {
                    @Override
                    public void run() {
                    	PopupBrowserDialog.this.setVisible(false);
                    	PopupBrowserDialog.this.dispose();
                    }
                });
            }
        });
	}
	
	private void initDynamicComponent() {
		topPosition.setLayout(new BoxLayout(topPosition, BoxLayout.Y_AXIS));
		
		JPanelWithHeight row1 = new JPanelWithHeight(40);
        row1.setLayout(new FlowLayout(FlowLayout.LEFT));
        
        row1.add(new JLabel("ESS用户名:"));
        final JTextField textEssUsername = new JTextField(15);
        row1.add(textEssUsername);
        
        row1.add(new JLabel("ESS密码:"));
        final JPasswordField textEssPassword = new JPasswordField(15);
        row1.add(textEssPassword);
        
        row1.add(new JLabel("考勤日期起:"));
        final JTextField textStartDate = new JTextField(DateUtil.format1(DateUtil.getLastMonthFirstDay()), 15);
//        final JDatePicker textStartDate = new JDatePicker(JDatePicker.STYLE_CN_DATE1, DateUtil.getLastMonthFirstDay());
        
        row1.add(textStartDate);
        
        row1.add(new JLabel("考勤日期止:"));
        final JTextField textEndDate = new JTextField(DateUtil.format1(DateUtil.getLastMonthLastDay()), 15);
//        final JDatePicker textEndDate = new JDatePicker(JDatePicker.STYLE_CN_DATE1, DateUtil.getLastMonthLastDay());
        row1.add(textEndDate);
        
        JButton btnLoad = new JButton("从ESS载入考勤表");
        JTextField textFilePath = new JTextField(30);
        btnLoad.addActionListener(new EssLoadActionListener(this, textEssUsername, textEssPassword, textStartDate, textEndDate, textFilePath, btnLoad));
        row1.add(btnLoad);
        
        row1.add(new JLabel("考勤记录表:"));
        JButton btnPrepareSelect = new JButton("...");
        JButton btnFill = new JButton("填充到表格");
        btnPrepareSelect.addActionListener(new PrepareActionListner(textFilePath, mainFrame));
        btnFill.addActionListener(new FillFormActionListener(textFilePath, browser, this));
        
        row1.add(textFilePath);
        row1.add(btnPrepareSelect);
        row1.add(btnFill);
        topPosition.add(row1);
		getContentPane().add(topPosition, BorderLayout.NORTH);
		topPosition.setVisible(false);
	}
}
